<?php

class ASolicitudBuses {

    public function __construct() {
	}
	public function creaCheckList(EFacade & $facade)
	{
		
		$docSB = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		$listaInstanciaBuses = & $docSB->listaInstanciaBuses;
		$cantidadPersonal = count($docSB->personalAsignado);
		$docSB->cantidadEmpleado = $cantidadPersonal;
		//$user->setAttribute('cantidad', $cantidadPersonal, 'tmp');
		$usuario=NULL;
		echo 'si ',count($docSB->listaInstanciaBuses);
		
		$ezpdo_=getManager();
		
		foreach($listaInstanciaBuses as $bus){
	        
			$ordenMantenimiento=$facade->session->create('DOrdenMCheckList');
			
			$ordenMantenimiento->asociadoA=$docSB;
			$ordenMantenimiento->instanciaBuses = $bus;
//				
			$role=NULL;
			$newEfacade =& EFacade :: initWorkflow($facade->session, $ordenMantenimiento, & $role);
			$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(),3);//tipo process
			$trans = $transSet->getTransByName('t1');
			$usuario=NULL;
			
			$newEfacade->fireTrans($trans, $usuario, "Crear",TRUE);
			echo "<br><br>la wt es: ";
			$ordenMantenimiento->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
			$newEfacade->saveWorkflow();
			$ordenMantenimiento->commit();echo "<br><br> es:1 ";
			$ordenMantenimiento->numero = $ordenMantenimiento->getDocumentNumber();
			$ordenMantenimiento->commit();
			echo "<br><br>es: ",$ordenMantenimiento;
			unset($newEfacade);
		
		}		echo "<br><br> es:2 ";
		$docSB->epSetDirty(TRUE);
		 $docSB;//die;
		$workflowSB = & $facade->getWorkflow();
		$workflowSB->epSetDirty(TRUE);
		//echo "<br>",$workflowOP;
		echo "<br><br> es:3 ";
		$transSetSB = & EFacade :: getCurrentTransSetAuto($workflowSB, /* MTrans :: $TYPE_AUTO_AGENT */ 2);

		$transSB = $transSetSB->getTransByName('t3');
//		echo "<br>Termina el transOP",$transOP;
		$facade->fireTrans($transSB,$usuario,'Creacion de Orden', TRUE);	
			echo "<br><br> es:4 ";
	
//		$workflowSB->commit();echo "<br><br> es:1 ";
//		$workflowSB->numero = $workflowSB->getDocumentNumber();
//		$workflowSB->commit();
				
	}
	
	public function verificar(EFacade & $facade)
	{
		echo '<hr>estoy en verificar ',$docSB = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		
		$ordenesMantenimiento = $docSB->ordenesMantenimiento;
		$cantidadOMCh = count($ordenesMantenimiento);
		
		$culminados = 0;
		foreach($ordenesMantenimiento as $OMCh){
			echo '<hr>ormch - ',$OMCh;
			
			$workflowOMCh = $OMCh->getWorkflowRef();
			//$facade->session->refresh($workflowOMCh);
			echo '<hr>wor - ',$workflowOMCh;
			$role=NULL;
			$newEfacade1 = & EFacade :: initWorkflowWithoutRole($facade->session, $OMCh, /* MTrans :: $TYPE_AUTO_PROCESS */ 2);
			echo '<hr>transSet - ',$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade1->getWorkflow(),2);//tipo process
			//$trans = $transSet->getTransByName('t1');
			
			$placeSetOMCh = & EFacade :: getCurrentPlaceSet($workflowOMCh);
			echo '<hr>  place ',$placeSetOMCh;
			
			if($placeSetOMCh->name == 'ps7'){
				
				echo 'si entro ';
				$cantidadOMCh--;
			}
			
			if($placeSetOMCh->name == 'ps5' || $placeSetOMCh->name == 'ps6'){
				
				echo 'si entro ';
				$culminados++;
			}
			//die('aki fue');
			//$workflowOMCh->saveWorkflow();
			
		} $aja = $cantidadOMCh.' - '.$culminados;
		echo '<hr>si ',$cantidadOMCh.' - '.$culminados;
		debug($aja);
		$usuario=NULL;
		if($cantidadOMCh == $culminados){
			
			echo '<hr> 1-->> ',$transSet = & EFacade :: getCurrentTransSetAuto($workflow,2);//tipo agent
			$trans = $transSet->getTransByName('t5');
			
			echo '<hr> 1trans ',$trans;
			//if()
			//die();
			$facade->fireTrans($trans, $usuario, "Crear",TRUE);
			$docSB->epSetDirty(TRUE);
			$facade->saveWorkflow();
			
			
		}
		else{
			
			echo '<hr> 2-->> ',$transSet = & EFacade :: getCurrentTransSetAuto($workflow,2);//tipo agent
			$trans = $transSet->getTransByName('t6');
			
			echo '<hr> 2trans ',$trans;
			//if()
			//die();
			$facade->fireTrans($trans, $usuario, "Crear",TRUE);
			$docSB->epSetDirty(TRUE);
			$facade->saveWorkflow();
			
		}
		
		$cantidadOMCh = 0;
		$culminados = 0;
	
		echo '<hr>12.si ',$cantidadOMCh,' - ',$culminados;
	}
	
	
	
}
?>